# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     __init__.py.py
   Description :
   Author :        ZhangYiLin
   date：          2020/12/01 12:57
-------------------------------------------------
   Change Activity:
                   2020/12/01 12:57
-------------------------------------------------
"""
# 模拟按键
import win32api
import win32con
import time


class KeyBoard(object):
    """模拟按键"""
    # 键盘码
    vk_code = {
        'backspace': 8,
        'tab': 9,
        'clear': 12,
        'enter': 13,
        'shift': 16,
        'ctrl': 17,
        'alt': 18,
        'pause': 19,
        'caps_lock': 20,
        'esc': 27,
        'spacebar': 32,
        'page_up': 33,
        'page_down': 34,
        'end': 35,
        'home': 36,
        'left_arrow': 37,
        'up_arrow': 38,
        'right_arrow': 39,
        'down_arrow': 40,
        'select': 41,
        'print': 42,
        'execute': 43,
        'print_screen': 44,
        'ins': 45,
        'del': 46,
        'help': 47,
        '0': 48,
        '1': 49,
        '2': 50,
        '3': 51,
        '4': 52,
        '5': 53,
        '6': 54,
        '7': 55,
        '8': 56,
        '9': 57,
        'a': 65,
        'b': 66,
        'c': 67,
        'd': 68,
        'e': 69,
        'f': 70,
        'g': 71,
        'h': 72,
        'i': 73,
        'j': 74,
        'k': 75,
        'l': 76,
        'm': 77,
        'n': 78,
        'o': 79,
        'p': 80,
        'q': 81,
        'r': 82,
        's': 83,
        't': 84,
        'u': 85,
        'v': 86,
        'w': 87,
        'x': 88,
        'y': 89,
        'z': 90,
        'numpad_0': 96,
        'numpad_1': 97,
        'numpad_2': 98,
        'numpad_3': 99,
        'numpad_4': 100,
        'numpad_5': 101,
        'numpad_6': 102,
        'numpad_7': 103,
        'numpad_8': 104,
        'numpad_9': 105,
        'multiply_key': 106,
        'add_key': 107,
        'separator_key': 108,
        'subtract_key': 109,
        'decimal_key': 110,
        'divide_key': 111,
        'f1': 112,
        'f2': 113,
        'f3': 114,
        'f4': 115,
        'f5': 116,
        'f6': 117,
        'f7': 118,
        'f8': 119,
        'f9': 120,
        'f10': 121,
        'f11': 122,
        'f12': 123,
        'f13': 124,
        'f14': 125,
        'f15': 126,
        'f16': 127,
        'f17': 128,
        'f18': 129,
        'f19': 130,
        'f20': 131,
        'f21': 132,
        'f22': 133,
        'f23': 134,
        'f24': 135,
        'num_lock': 144,
        'scroll_lock': 145,
        'left_shift': 160,
        'right_shift ': 161,
        'left_control': 162,
        'right_control': 163,
        'left_menu': 164,
        'right_menu': 165,
        'browser_back': 166,
        'browser_forward': 167,
        'browser_refresh': 168,
        'browser_stop': 169,
        'browser_search': 170,
        'browser_favorites': 171,
        'browser_start_and_home': 172,
        'volume_mute': 173,
        'volume_down': 174,
        'volume_up': 175,
        'next_track': 176,
        'previous_track': 177,
        'stop_media': 178,
        'play/pause_media': 179,
        'start_mail': 180,
        'select_media': 181,
        'start_application_1': 182,
        'start_application_2': 183,
        'attn_key': 246,
        'crsel_key': 247,
        'exsel_key': 248,
        'play_key': 250,
        'zoom_key': 251,
        'clear_key': 254,
        '+': 187,
        ',': 188,
        '-': 189,
        '.': 190,
        '/': 191,
        '`': 192,
        ';': 186,
        '[': 219,
        '\\': 220,
        ']': 221,
        "'": 222}

    @staticmethod
    def key_down(key_name):
        """按下键"""
        key_name = key_name.lower()
        try:
            win32api.keybd_event(KeyBoard.vk_code[key_name], 0, 0, 0)
        except Exception as e:
            print(e)

    @staticmethod
    def key_up(key_name):
        """抬起键"""
        key_name = key_name.lower()
        win32api.keybd_event(
            KeyBoard.vk_code[key_name],
            0,
            win32con.KEYEVENTF_KEYUP,
            0)

    @staticmethod
    def one_key(key):
        """模拟单个按键"""
        key = key.lower()
        KeyBoard.key_down(key)
        time.sleep(2)
        KeyBoard.key_up(key)

    @staticmethod
    def multiple_buttons(**kwargs):
        """
        按下多个按键
        """
        for key in kwargs:  # 按下所有按键
            KeyBoard.key_down(kwargs[key].lower())
        for key in kwargs:   # 释放所有按键
            KeyBoard.key_up(kwargs[key].lower())


if __name__ == '__main__':
    import time

    time.sleep(5)
    key_board = KeyBoard()
    key_board.multiple_buttons(key1='a', key2='b')
    key_board.multiple_buttons(a='F12')
    key_board.multiple_buttons(a='ctrl', b='shift', c='m')
